﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLNSClient
{
    public partial class Frm_DVHC : DevExpress.XtraEditors.XtraForm
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public Frm_DVHC()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            txt_MaDVHC.Enabled = false;
            txt_TenDVHC.Enabled = false;

            gv_dsDVHC.DataSource = sv.getDVHC();
            gridView1.Columns[0].Caption = "Mã đơn vị hành chính";
            gridView1.Columns[1].Caption = "Tên đơn vị hành chính";
        }

        private void Frm_DVHC_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            List<QLNSServiceReference.DONVI_HANHCHINH> listLastID = new List<QLNSServiceReference.DONVI_HANHCHINH>(sv.getLastDVHC());
            string a = listLastID[0].Ma_DV_HC;
            int vt = a.IndexOf("C");
            string lastnum = a.Substring(vt + 1, 3);
            int tempID = Int32.Parse(lastnum);
            int newnum = tempID + 1;

            string newid;
            if (newnum < 10)
            {
                newid = "DVHC0" + newnum.ToString();
                txt_MaDVHC.Text = newid;
            }
            else
            {
                newid = "DVHC" + newnum.ToString();
                txt_MaDVHC.Text = newid;
            }

            txt_TenDVHC.Text = "";
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            string tempMaDVHC = txt_MaDVHC.Text;
            string tempTenDVHC = txt_TenDVHC.Text;

            QLNSServiceReference.DONVI_HANHCHINH dvhc = new QLNSServiceReference.DONVI_HANHCHINH();
            dvhc.Ma_DV_HC = tempMaDVHC;
            dvhc.TenDVHC = tempTenDVHC;

            if (demsua == 1)
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn cập nhật lại thông tin?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sv.updateDVHC(dvhc);
                    LoadData();
                    demsua = 0;
                    MessageBox.Show("Cập nhật thông tin đơn vị '"+txt_TenDVHC.Text+"' mã '"+txt_MaDVHC.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                int kq = sv.addDVHC(dvhc);
                if (kq != -1)
                {
                    LoadData();
                    MessageBox.Show("Thêm đơn vị '"+txt_TenDVHC+"' mã '"+txt_MaDVHC.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;
            
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            txt_MaDVHC.Enabled = false;
            txt_TenDVHC.Enabled = false;
           
        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            demsua = 1;
            
            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;

            txt_TenDVHC.Enabled = true;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
        }

        private void gv_dsDVHC_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            txt_MaDVHC.Enabled = false;
            txt_TenDVHC.Enabled = false;

            try
            {
                txt_MaDVHC.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_TenDVHC.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
            }
            catch { }
        }
    }
}